<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class UserCheckSignInLog extends Base
{
    protected $table = 'user_check_signin_log';
    protected $user_sign_cache = 'CACHE:SIGN:USER:';

    public $toDayStartTime;
    public $toDayEndTime;

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);

        $this->toDayStartTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $this->toDayEndTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
    }

    /**
     * 创建记录
     *
     * @param int $userId 用户ID
     * @return bool
     */
    public function createLog($userId, $is_new = 2)
    {
        return DB::table($this->table)->insertGetId(['user_id' => $userId, 'created_at' => time(), 'is_new' => $is_new]);
    }

    /**
     * 查询今内日数据
     *
     * @param int $userId 用户ID
     * @return bool
     * */
    public function getToDayLogData($userId)
    {
        $data = DB::table($this->table)->where([
            ['user_id', '=', $userId],
            ['created_at', '>=', $this->toDayStartTime],
            ['created_at', '<=', $this->toDayEndTime],
        ])->first(['id']);
        return self::objToArray($data);
    }

    /**
     * 查询昨天数据
     *
     * @param int $userId 用户ID
     * @param int $newType 0为老用户 1为萌新
     * @return bool
     * */
    public function getYesterdayLogData($userId)
    {
        $startTime = strtotime(date('Y-m-d 00:00:00', strtotime('-1 day')));
        $endTime = strtotime(date('Y-m-d 23:59:59', strtotime('-1 day')));
        $data = DB::table($this->table)->where([
            ['user_id', '=', $userId],
            ['created_at', '>=', $startTime],
            ['created_at', '<=', $endTime],
        ])->first(['id']);
        $data = self::objToArray($data);

        // 判断 昨日数据不存在 并且 今天未签到 则删除该用户所有数据 从新开始
        if (empty($data)) {
            $getToDayLogData = $this->getToDayLogData($userId);
            if (empty($getToDayLogData)) {
                $this->removeLog($userId);// 删除用户数据
            }
        }
        return $data;
    }

    /**
     * 查询签到数据(萌新)
     * @param int $userId 用户ID
     * @return bool
     * */
    public function getNoviceLogData($userId)
    {
        $upDay = strtotime(date('Y-m-d 00:00:00', strtotime('-1 day')));
        $data = DB::table($this->table)->where([
            ['user_id', '=', $userId],
        ])->orderBy('created_at', 'desc')->first(['id', 'created_at']);
        $data = self::objToArray($data);
        $retData['status'] = 0;//未签到
        $cacheKey = $this->user_sign_cache . $userId;
        if ($data) { //存在签到记录
            if ($this->getNoviceCache($userId) != 2) {
                Redis::set($cacheKey, 1);
            }
            $retData['status'] = 1; //有签到
            //存在签到 判断签到是否昨天
            if ($data['created_at'] < $upDay) {
                //断签
                Redis::set($cacheKey, 2);
                $retData['status'] = 2;//断签
            }
        } elseif (Redis::get($cacheKey) != 2) {
            // 不存在签到记录
            Redis::set($cacheKey, 1);
        }
        $retData['cache'] = Redis::get($cacheKey);
        return $retData;
    }

    public function getNoviceCache($userId, $num = 2)
    {
        $cacheKey = $this->user_sign_cache . $userId;
        $cache = Redis::get($cacheKey);
        if (is_null($cache)) {
            Redis::set($cacheKey, $num);
        }
        return Redis::get($cacheKey);
    }

    /**
     * 查询7日内日数据
     *
     * @param int $userId 用户ID
     * @param int $removeFlag 删除标识
     * @return array
     * */
    public function getSevenDayLogData($userId, &$removeFlag)
    {
        $sevenDayBeforeTime = strtotime(date('Y-m-d 00:00:00', strtotime('-7 day')));

        $data = DB::table($this->table)->where([
            ['user_id', '=', $userId],
            ['created_at', '>=', $sevenDayBeforeTime],
            ['created_at', '<=', $this->toDayEndTime],
        ])->orderBy('created_at', 'asc')->get()->toArray();
        $data = self::objToArray($data);

        // 判断 数据满足7条 并且 今天未签到 则删除该用户所有数据 从新开始
        if (count($data) >= 7) {
            $getToDayLogData = $this->getToDayLogData($userId);
            if (empty($getToDayLogData)) {
                $removeFlag = 1;
                $this->removeLog($userId);// 删除用户数据
            }
        }

        return $data;
    }


    /**
     * 查询7日内日数据
     *
     * @param int $userId 用户ID
     * @return array
     * */
    public function getUserSevenDayLogData($userId)
    {
        $sevenDayBeforeTime = strtotime(date('Y-m-d 00:00:00', strtotime('-7 day')));
        $data = DB::table($this->table)->where([
            ['user_id', '=', $userId],
            ['created_at', '>=', $sevenDayBeforeTime],
            ['created_at', '<=', $this->toDayEndTime],
        ])->orderBy('id', 'asc')->get()->toArray();
        $data = self::objToArray($data);
        return $data;
    }



    /**
     * 查询所有数据
     *
     * @param int $userId 用户ID
     * @return array
     * */
    public function getUserAllDayLogData($userId)
    {
        $data = DB::table($this->table)->where([
            ['user_id', '=', $userId],
        ])->orderBy('id', 'asc')->get()->toArray();
        $data = self::objToArray($data);
        return $data;
    }

    /**
     * 删除数据
     *
     * @param int $userId 用户ID
     * @return bool
     * */
    public function removeLog($userId)
    {
        return DB::table($this->table)->where(['user_id' => $userId])->delete();
    }
}